//
//  GridViewItemGuest.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-20.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "GridViewItemGuest.h"
#import "Common.h"
#import "FileManager.h"

@implementation GridViewItemGuest
-(id) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //设置UIView的背景图片
        imageBkg.image = [FileManager getImage:@"pluplu_bkg.png" type:3 withRect:imageBkg.frame];
        [labelName setTextAlignment:UITextAlignmentLeft];//菜名左对齐
        [labelName setFont:[UIFont fontWithName:@"Arial" size:15.0f]];//字体大小
        
        imageViewSelected = [[UIImageView alloc] init];
        [imageViewSelected setTag:TagOfUIImageView+2];
        imageViewSelected.image = [FileManager getImage:@"pluselected_img.png" type:3 withRect:imageViewSelected.frame];
        
        labelPrice = [[UILabel alloc] init];
        [labelPrice setTag:TagOfUILabel+1];
        [labelPrice setAdjustsFontSizeToFitWidth:YES];//自动调整字体大小
        [labelPrice setFont:[UIFont fontWithName:@"Arial" size:15.0f]];//字体大小
        [labelPrice setTextColor:[UIColor whiteColor]];//字体白色
        [labelPrice setTextAlignment:UITextAlignmentRight];//价格右对齐
        [labelPrice setBackgroundColor:[UIColor clearColor]];//没有背景颜色
        
        [self addSubview:imageViewSelected];
        [self addSubview:labelPrice];
    }
    return self;
}

//-(id) initWithObject:(id)data{
//    pluPlu = (PluPlu *)data;
//    self = [self initWithFrame:CGRectMake(0, 0, 0, 0)];
//    return self;
//}
- (void)drawRect:(CGRect)rect{
    [imageBkg setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [imageView setFrame:CGRectMake(10, 10, self.frame.size.width-20, self.frame.size.height-60)];
    [imageRecommend setFrame:CGRectMake(imageView.frame.size.width-16, imageView.frame.size.height-16, 26, 26)];
    [labelName setFrame:CGRectMake(10, self.frame.size.height-40, imageView.frame.size.width-60, 25)];
    
    [imageViewSelected setFrame:CGRectMake(0, 0, 40, 40)];
    [labelPrice setFrame:CGRectMake(labelName.frame.origin.x + labelName.frame.size.width, 
                                    labelName.frame.origin.y,
                                    imageView.frame.size.width - labelName.frame.size.width, 
                                    labelName.frame.size.height)];
    
     [imageView setImage:[FileManager getImage:pluPlu.ppImage type:0 withRect:imageView.bounds]]; 
}

-(void) setData:(id)data{
    pluPlu = (PluPlu *)data;
    [imageView setImage:[FileManager getImage:pluPlu.ppImage type:0 withRect:imageView.bounds]]; 
    [labelName setText:JsonField(pluPlu.ppDescripts)];
    if (pluPlu.ppPluPrices != nil && [pluPlu.ppPluPrices count] > 0) {
        PluPrice *pluPrice = [pluPlu.ppPluPrices objectAtIndex:0];
        [labelPrice setText:PriceFormatter(pluPrice.price,JsonField(pluPrice.units))];      
    }else {
        [labelPrice setText:PriceFormatter(0,nil)];
    }
    //设置推荐菜标志
    if ([pluPlu.ppRecommended isEqualToString:@"0"]) {
        imageRecommend.hidden = YES;
    }else {
        imageRecommend.hidden = NO;
    }
    
    //设置选中标志
    float totalNumber = [DataBaseAccess getPluPluCount:pluPlu.ppCode];
    if (totalNumber == 0) {
        [self setSelectedItem:NO];
    }else {
        [self setSelectedItem:YES];
    }
}
- (void) setSelectedItem:(BOOL)selected{
    if (selected) {
        imageViewSelected.hidden = NO;
    }else {
        imageViewSelected.hidden = YES;
    }
}

-(void) dealloc{
    MyNSLog(@"GridViewItemGuest[%d] is deallocing...", self.tag-TagOfUIGridViewItem);
    [imageViewSelected release];
    [labelPrice release];
    [super dealloc];
}
@end
